package com.huiyan.hysys.third;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.huiyan.hysys.config.SystemConfig;
import com.huiyan.hysys.util.DateUtil;

import java.io.InputStream;
import java.util.Date;

public class AliOssUtil {

    //平台的密钥
    private static final String accessKeyId = "LTAI5t8QA19CRR2V15oHyHW8";
    private static final String accessKeySecret = "OqUKmRFUnAYm0frzUc43uKnk5BNoaP";
    public static final String bucket = "data213";
    private static final String endpoint = "oss-cn-beijing.aliyuncs.com";
    private static  OSS client;
    static {
        client = new OSSClientBuilder().build(endpoint,accessKeyId,accessKeySecret);

    }

    public static String upload(String objName, InputStream is){

        //上传文件到云存储中心
        client.putObject(bucket,objName,is);
        //生成访问地址
        return createUrl(objName);

    }

    /**
     * 上传文件
     * @param objName
     * @param date
     * @param is
     * @return
     */
    public static String upload(String objName, Date date,InputStream is){

        //上传文件到云存储中心
        client.putObject(bucket,objName,is);
        //生成访问地址
        return createUrl(objName,date);
    }


    /**
     * 验证对象名称是否存在
     * @param objName
     * @return
     */
    public static boolean checkName(String objName){
        return client.doesObjectExist(bucket,objName);
    }


    /**
     * 删除文件
     * @param
     * @return
     */
    public static boolean delObject(String objName){
        if (client.doesObjectExist(bucket,objName)){
            client.deleteObject(bucket,objName);
            return true;
        }
        return false;
    }



    public static String createUrl(String objName){
        return client.generatePresignedUrl(bucket,objName, DateUtil.getDate(SystemConfig.OSS_URL_DAYS)).toString();
    }

    public static String createUrl(String objName,Date date){
        return client.generatePresignedUrl(bucket,objName,date).toString();
    }

}
